package com.kelp.codetoolkit.entity

import com.intellij.lang.java.JavaLanguage
import com.intellij.navigation.NavigationItem
import com.intellij.openapi.fileEditor.OpenFileDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiMethod
import com.intellij.psi.impl.light.LightElement
import com.kelp.codetoolkit.respository.CallerHistoryManager

/**
 * @author: liuyuebai
 * @date: 2025/8/10 13:37
 * @description:
 */
class GoBackPsiElement(manager: PsiManager, private val project: Project, private val targetMethod: PsiMethod): LightElement(manager, JavaLanguage.INSTANCE), NavigationItem {
    override fun toString() = "GoBackElement"
    override fun getText() = "Return To Last Caller"

    override fun navigate(requestFocus: Boolean) {
        CallerHistoryManager.getLastCaller(targetMethod)?.let { pos ->
            OpenFileDescriptor(project, pos.file, pos.offset).navigate(true)
        }
        CallerHistoryManager.clearCaller(targetMethod)
    }

    override fun canNavigate() = true
    override fun canNavigateToSource() = true
}